<?php

namespace App\Models\CacheModel\zl4;

use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class Zl4HoroscopeUserConfig extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl4_horoscope_user_config';

    protected $cacheModel = 'horoscope.user_config';

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {   // 获取未删除 的数据
                $data = $this
                    ->where(['uid' => ['in', $ids]])
                    ->column('id,config,create_time,update_time', 'uid');
            } else
            {
                $data = [];
            }
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'uid', 'update_time', 'create_time']);

            if (isset($itemData['config']))
            {
                $data[$index]['config'] = $itemData['config'] ? json_decode($itemData['config'], true) : [];
            }
        }

        return $data;
    }

    /**
     * 获取用户 八字排盘 配置信息
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public function getUserConfig(int $uid): array
    {
        $userConfig = $this->getInfoById($uid, ['id', 'config']);

        if (!$userConfig)
        {
            $userConfig = [
                'id' => 0,
                'config' => [
                    'time_model1' => 2, // 1=>北京时间 2=>真太阳时
                    'time_model2' => 1,
                    'shensha' => 2,
                    'liuyun' => 1,
                    'color' => 0,
                ]
            ];
        } else
        {
            if (!isset($userConfig['config']['time_model1']))
            {
                $userConfig['config']['time_model1'] = 2; // 1=>北京时间 2=>真太阳时
            }
            if (!isset($userConfig['config']['time_model2']))
            {
                $userConfig['config']['time_model2'] = 1;
            }
            if (!isset($userConfig['config']['shensha']))
            {
                $userConfig['config']['shensha'] = 2;
            }
            if (!isset($userConfig['config']['liuyun']))
            {
                $userConfig['config']['liuyun'] = 1;
            }
            if (!isset($userConfig['config']['color']))
            {
                $userConfig['config']['color'] = 0;
            }
        }

        return $userConfig;
    }

    /**
     * 设置缓存
     * @param int $uid
     * @param array $data
     * @return bool
     */
    public function setUserConfig(int $uid, array $data)
    {
        try
        {
            if ($data['id'] === 0)
            {
                $this->save([
                    'uid' => $uid,
                    'config' => json_encode($data['config'], JSON_UNESCAPED_UNICODE),
                    'create_time' => time()
                ]);
            } else
            {
                $this->where(['id' => $data['id']])->update([
                    'config' => json_encode($data['config'], JSON_UNESCAPED_UNICODE),
                    'update_time' => time()
                ]);
            }
            // 删除配置缓存
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->rmCache();
        }
        catch (\Exception $e)
        {
            Log::write('Zl4HoroscopeUserConfig model => setUserConfig()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
            return false;
        }

        return true;
    }
}
